package com.sinotn.ks.node.sign.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sinotn.SinotnLogger;
import com.sinotn.error.MessageException;
import com.sinotn.ks.definition.ExamEngine;
import com.sinotn.ks.definition.Examinee;
import com.sinotn.ks.node.runtime.bean.LoginUserBean;
import com.sinotn.ks.node.sign.service.FaceService;
import com.sinotn.web.ResponseUtil;

@Controller
@RequestMapping(value = "/R/plugin/sign/face")
public class FaceAction {
	@Autowired
	private FaceService faceService;
	@Autowired
	private ExamEngine examEngine;

	/**
	 * 执行人脸比对校验
	 * @param request
	 * @param response
	 * @auth <a href="mailto:xieqj@sinotn.com">谢启进</a>
	 * @date 2018年5月14日 下午4:06:08
	 */
	@RequestMapping(value = "/valid.jspx")
	public void valid(HttpServletRequest request, HttpServletResponse response) {
		String id=request.getParameter("id");
		try{
			Examinee examinee=this.examEngine.getExaminee(id);
			if(examinee==null){
				throw new MessageException("不存在开考考生信息:"+id);
			}
			String roomId=request.getParameter("roomId");
			if(!examinee.getRoomId().equals(roomId)){
				throw new MessageException("考生考场信息不匹配");
			}
			String text=request.getParameter("text");//base64照片信息
			String ip=request.getParameter("ip");
			String mac=request.getParameter("mac");
			LoginUserBean loginUser=this.getLoginUser(request);
			int similarVal=this.faceService.similarCertify(examinee,ip,mac,null,text,loginUser.getAccount());
			StringBuilder sb=new StringBuilder();
			sb.append("{\"state\":0");
			sb.append(",\"similar\":").append(similarVal);
			sb.append("}");
			ResponseUtil.renderJson(response, sb.toString());
		}catch(MessageException e){
			ResponseUtil.renderJson(response, "{\"state\":1,\"msg\":\""+e.getMessage()+"\"}");
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("执行人脸比对校验发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"state\":1}");
		}
	}

	/**
	 * 设置人脸比对阀值
	 * @param request
	 * @param response
	 * @auth <a href="mailto:xieqj@sinotn.com">谢启进</a>
	 * @date 2018年5月14日 下午4:26:47
	 */
	@RequestMapping(value = "/setThreshold.jspx")
	public void setThreshold(HttpServletRequest request, HttpServletResponse response) {
		try{
			int threshold=Integer.valueOf(request.getParameter("threshold"));
			this.faceService.setThreshold(threshold);
			ResponseUtil.renderJson(response, "{\"state\":0}");
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("设置人脸比对阀值发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"state\":1}");
		}
	}
	/**
	 * 获取人脸比对阀值
	 * @param request
	 * @param response
	 * @auth <a href="mailto:xieqj@sinotn.com">谢启进</a>
	 * @date 2018年5月14日 下午4:27:00
	 */
	@RequestMapping(value = "/getThreshold.jspx")
	public void getThreshold(HttpServletRequest request, HttpServletResponse response) {
		try{
			int threshold=this.faceService.getThreshold();
			StringBuilder sb=new StringBuilder();
			sb.append("{\"state\":0");
			sb.append(",\"value\":").append(threshold);
			sb.append("}");
			ResponseUtil.renderJson(response, sb.toString());
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("获取人脸比对阀值发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"state\":1}");
		}
	}

	/**
	 * 获取当前登录会话用户信息。
	 * @param request
	 * @return
	 * @auth <a href="mailto:xieqj@sinotn.com">谢启进</a>
	 * @date 2017年4月1日 下午1:06:43
	 */
	private LoginUserBean getLoginUser(HttpServletRequest request){
		HttpSession httpSession=request.getSession(false);
		if(httpSession==null){
			return null;
		}
		return (LoginUserBean)httpSession.getAttribute("_SessionUserKey");
	}
}
